package com.example.springboot_system.Controller;

import com.example.springboot_system.pojo.vo.GetDepartmentRspVO;
import com.example.springboot_system.service.AdminService;
import com.example.springboot_system.service.AnnounceService;
import com.example.springboot_system.pojo.User;
import com.example.springboot_system.service.RoleService;
import com.example.springboot_system.service.UserService;
import com.example.springboot_system.utils.Email;
import com.example.springboot_system.utils.PhoneCode;
import com.example.springboot_system.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.net.UnknownHostException;
import java.util.Random;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private AnnounceService announceService;
    @Autowired
    private RoleService roleService ;

    @RequestMapping("/login")
    public String toLogin(HttpSession session){

        roleService.findAllRole(session);
        return "login";
    }

    @RequestMapping("/logout")
    @ResponseBody
    public String logout(){
        return "success";
    }

    @RequestMapping("/doLogin")
    @ResponseBody
   /* public ResponseResult doLogin(String email, String plainPassword, HttpSession session){
        ResponseResult result = userService.doLogin(email, plainPassword, session);
        announceService.findAllAnnounceIf1(session);*/
    public ResponseResult doLogin(String email, String plainPassword, HttpSession session,HttpServletRequest request) throws UnknownHostException {
        ResponseResult result = userService.doLogin(email, plainPassword, session,request );
            announceService.findAllAnnounceIf1(session);
        return result;
    }
    //跳转到主页面
    @RequestMapping("/toMain")
    public String toMain(HttpSession session){
        announceService.findAllAnnounceIf1(session);
        return "main";
    }

    @RequestMapping(value = "/getDepartment")
    @ResponseBody
    public List<GetDepartmentRspVO> getDepartment() {
        return adminService.getDepartment();
    }
   //跳转到角色审核部分
   @RequestMapping("/toApply")
   public String toExamine(){
       return "RoleApply";
   }
   //跳到找回密码页面
   @RequestMapping("/loginByPhone")
   public String loginByPhone(){
       return "loginByPhone";
   }

    //跳到找回密码页面
    @RequestMapping("/t1")
    public String t1(){
        return "accountset1";
    }




    //跳转到账号设置页面
    @RequestMapping("/toAccountSettings")
   public  String toAccountSettings(HttpSession session,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
         userService.findUserLoginInfo(session,pageNum);
         userService.findUserRole(session);
        return "accountset";
    }


    @RequestMapping("/doRegister")
    @ResponseBody
    public ResponseResult doRegister(User user, String code, String description,HttpSession session){
        ResponseResult result = userService.doRegister(user,code,description,session);
        return result;
    }
    //发送邮件
    @RequestMapping("/sendEmail")
    @ResponseBody
    public ResponseResult sendEmail(String email, HttpSession session){
        System.out.println(email);
        String result = Email.sendEmail(email);
        if (result==null){
            return new ResponseResult(200,"发送失败");
        }
        session.setAttribute("email1",result);
        System.out.println(result);
        return new ResponseResult(200,"发送成功注意查收");
    }

   //检查邮箱是否存在
    @RequestMapping("/checkEmail")
    @ResponseBody
    public ResponseResult checkEmail(String email){
        ResponseResult result = userService.checkEmail(email);
        return result;
    }
    //检查用户名是否存在
    @RequestMapping("/checkUsername")
    @ResponseBody
    public ResponseResult checkUsername(String username){
        ResponseResult result = userService.checkUsername(username);
        return result;
    }

    //获取个人中心
    @GetMapping("/toUserInfo")
    public String toUserInfoPage(HttpSession session, Model model) {
        //根据当前用户的uid 或者username 查询用户信息 绑定回页面
        //将对象绑定到userdata页面
        String id = (String) session.getAttribute("id");
        User user = userService.findUserInfoByUserId(id);
        model.addAttribute("user", user);
        System.out.println("绑定的用户数据为："+user);
        return "userInformation";

    }
    //更新用户的数据
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public ResponseResult updateUserInfo(User user){
        System.out.println("需要更新的数据为:"+user);
        ResponseResult result = userService.updateUserInfo(user);
        return result;
    }
    //检查旧密码是否正确
    @RequestMapping("/checkPwd")
    @ResponseBody
    public  ResponseResult checkPwd(String oldPassword,HttpSession session){
        System.out.println(oldPassword);
        ResponseResult result = userService.checkPwd(oldPassword, session);
        return  result;
    }

    @PostMapping("/doUpdatePwd")
    @ResponseBody
    //更新密码
    public ResponseResult doUpdatePwd(String oldPassword, String newPassword, String okPassword, HttpSession session) {

        ResponseResult result = userService.updatePassword(oldPassword, newPassword, okPassword, session);
        return result;
    }
    //查询用户所拥有的角色
    @RequestMapping("/findUserRole")
    @ResponseBody
    public ResponseResult findUserRole(HttpSession session) {
        ResponseResult result = userService.findUserRole(session);
        return result;
    }



    //给用户添加角色
    @PostMapping("/addUserRole")
    @ResponseBody
    public ResponseResult addUserRole(String description,HttpSession session) {
        ResponseResult result = userService.addUserRole(description, session);
        return result;
    }

    //获取审核角色
    @RequestMapping("/getRoleApply")

    public String getRoleApply(User user ,HttpSession session) {
        String result = userService.getRoleApply(session);
        return result;
    }
    //审核通过
    @RequestMapping("/Approved")
    @ResponseBody
    public ResponseResult Approved(String username,HttpSession session) {
        System.out.println(username);
        ResponseResult result = userService.Approved(username, session);
        return result;
    }
    //审核不通过
    @RequestMapping("/NoApproved")
    @ResponseBody
    public ResponseResult NoApproved(String username,HttpSession session) {
        ResponseResult result = userService.NoApproved(username, session);
        return result;
    }
    //手机发送验证码
    @RequestMapping("/sendCode")
    @ResponseBody
    public ResponseResult sendCode(String phone, HttpSession session) throws Exception {
        if(phone==null){
            return new ResponseResult(201,"电话号码不能为空");
        }
        StringBuffer stringBuffer = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int nextInt = random.nextInt(9);
            stringBuffer.append(nextInt);
        }
        String result1 = stringBuffer.toString();
        PhoneCode phoneCode = new PhoneCode();
        String result=phoneCode.sms(phone,result1);
        if (result==null){
            return new ResponseResult(201,"发送失败");
        }
        session.setAttribute("phoneCode",result);
        return new ResponseResult(200,"发送成功注意查收");
    }

   //手机号码登录
   @RequestMapping("/doLoginByPhone")
   @ResponseBody
   public ResponseResult doLoginByPhone(String phone, String code, HttpSession session){
       ResponseResult result = userService.doLoginByPhone(phone, code, session);
       return result;
   }
   //查询登录信息
   @RequestMapping("/findUserLoginInfo")
   @ResponseBody
   public ResponseResult findUserLoginInfo( HttpSession session,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
       ResponseResult result = userService.findUserLoginInfo(session,pageNum);
       return result;
   }

    //检查手机号码是否正确
    @RequestMapping("/checkPhone")
    @ResponseBody
    public  ResponseResult checkPhone(String phone,HttpSession session){
        System.out.println(phone);
        ResponseResult result = userService.checkPhone(phone, session);
        return  result;
    }
}
